<template>
	
        <el-row :gutter="10">
            <el-col :span="6">
                <xm-func-select v-if="xmProductCpd"  :xm-product="xmProductCpd" @check-change="onXmFuncRowClick" />
            </el-col>
            <el-col :span="18">
                <ContentWrap>
                    <template #header>
                <el-space wrap>

                    <mdp-hi-query ref="hiQueryBtn"
          :column-configs="columnConfigs"
          v-model="hiQueryParams"
          @change="onHiQueryParamsChange"
        />
        <el-button icon="zoom-out" @click="searchReset()" title="重置查询条件并查询" ref="searchResetBtn"/>
  

        <el-button icon="download" @click="export2Excel()" title="导出当前结果数据"  plain ref="downloadBtn"/>
        <mdp-table-configs :column-configs="columnConfigs" v-model="checkedColumns" ref="columnConfigsBtn"/>  
  
								 
 
                        <el-button type="primary" @click="showAdd" icon="plus" plain/>
                        <el-button type="danger" v-loading="load.del" @click="batchDel" :disabled="this.sels.length===0 || load.del==true" icon="delete" plain/>
                 </el-space> 
                </template>
                <el-space wrap>
                    <mdp-select placeholder="用例状态" clearable item-code="testCaseStatus" v-model="filters.caseStatus" effect="dark"/>
                    <mdp-select placeholder="测试方式" clearable item-code="testType" v-model="filters.testType" effect="dark"/>

                    <el-input v-model="filters.caseName" style="width: 20%;" placeholder="名称 按回车" @keyup.enter="searchTableDatas" clearable/>

                    <el-button v-loading="load.list" :disabled="load.list==true" @click="searchTableDatas" icon="search" type="primary">查询</el-button>

                </el-space>
           
            </ContentWrap>
                    <!--列表 XmTestCase 测试用例-->
                    <el-table ref="table" :data="tableDatas" v-adaptive="{bottom:50}" @sort-change="sortChange" highlight-current-row v-loading="load.list" border @selection-change="selsChange" @row-click="rowClick" style="width: 100%;">
                        <el-table-column  type="selection" width="55" show-overflow-tooltip fixed="left" />
                         <!--
                        <el-table-column sortable prop="username" width="55" show-overflow-tooltip  fixed="left">
                            <span class="cell-text">  {{scope.row.username}}}  </span>
                            <span class="cell-bar"><el-input style="display:inline;" v-model="scope.row.username" placeholder="" @change="editSomeFields(scope.row,'username',$event)" :maxlength="22"/></span>
                        </el-table-column>
                        -->
                         <el-table-column prop="caseName" label="标题" min-width="250">
                            <template #default="scope">
                                <span> <el-link  @click="showEdit( scope.row,scope.$index)">{{scope.row.caseName}} </el-link></span>
                                <span class="tool-bar">
                                     <el-button @click="copy(scope.row,scope.$index)" icon="document-copy" circle title="复制一行"/>
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="caseStatus" label="状态" width="120" show-overflow-tooltip>
                            <template #default="scope">
                                <mdp-select show-style="tag"  item-code="testCaseStatus" v-model="scope.row.caseStatus" effect="dark" @change="editSomeFields(scope.row,'caseStatus',$event)"/>
                            </template>
                        </el-table-column>

                        <el-table-column prop="testType" label="测试方式" min-width="120" show-overflow-tooltip>
                            <template #default="scope">
                                <mdp-select show-style="tag"  item-code="testType" v-model="scope.row.testType" effect="dark" @change="editSomeFields(scope.row,'testType',$event)"/>
                            </template>
                        </el-table-column>
                        <el-table-column prop="funcName" label="模块" min-width="150" show-overflow-tooltip>
                            <template #default="scope">
                                <span> {{scope.row.funcName}} </span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="verNum" label="版本号" min-width="80" show-overflow-tooltip>
                            <template #default="scope">
                                <span> {{scope.row.verNum}} </span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="cusername" label="负责人姓名" min-width="120" show-overflow-tooltip>
                            <template #default="scope">
                                <mdp-select-user show-style="tag"   v-model="scope.row.cuserid" :init-name="scope.row.cusername" @change2="editSomeFields(scope.row,'cuserid',$event)"/>
                            </template>
                        </el-table-column>

                    </el-table>
                    <el-pagination  layout="total, sizes, prev, pager, next" @current-change="handleCurrentChange" @size-change="handleSizeChange" :page-sizes="[10,20, 50, 100, 500]" :current-page="pageInfo.pageNum" :page-size="pageInfo.pageSize"  :total="pageInfo.total" style="float:right;" />
 

            </el-col>
        </el-row>

		<el-row>
			<!--编辑 XmTestCase 测试用例界面-->
			<mdp-dialog ref="editDlg" title="编辑测试用例" v-model="editFormVisible" fullscreen width="90%" top="20px"  append-to-body   :close-on-click-modal="false">
			   <template #default="{data,visible,dialog}">
                <xm-test-case-edit op-type="edit" :xm-func="xmFunc" :xm-test-case="data.formData" :visible="visible" :xm-test-casedb="xmTestCasedb" @cancel="dialog.close()" @submit="($event)=>{afterEditSubmit($event),dialog.close()}" @edit-fields="onEditFields" />

               </template>
			</mdp-dialog>

			<!--新增 XmTestCase 测试用例界面-->
			<mdp-dialog ref="addDlg" title="新增测试用例" v-model="addFormVisible" fullscreen width="90%" top="20px"  append-to-body  :close-on-click-modal="false">
                <template #default="{data,visible,dialog}">
                    <xm-test-case-edit op-type="add" :xm-func="xmFunc" :xm-menu="data.formData" :xm-product="xmProduct" :xm-test-casedb="xmTestCasedb" :visible="visible" @cancel="dialog.close()" @submit="($event)=>{afterAddSubmit($event);dialog.close()}" />

               </template>
			</mdp-dialog>
	    </el-row>
	
</template>

<script>

import util from '@/components/mdp-ui/js/util';//全局公共库
import config from '@/api/mdp_pub/mdp_config';//全局公共库
import { listXmTestCase, delXmTestCase, batchDelXmTestCase,editSomeFieldsXmTestCase,addXmTestCase } from '@/api/xm/core/xmTestCase';
import  XmTestCaseEdit from './XmTestCaseEdit.vue';//新增修改界面
import  XmFuncSelect from '../xmFunc/XmFuncSelect.vue';//新增修改界面

import { mapState } from 'pinia'
import { useUserStore } from '@/store/modules/user' 
import { MdpTableMixin } from '@/components/mdp-ui/mixin/MdpTableMixin';
export default {
    mixins:[MdpTableMixin],
    name:'xmTestCaseMng',
    components: {
        XmTestCaseEdit,XmFuncSelect
    },
    props:['visible','xmTestCasedb','scene','xmMenu','xmProduct'],
    computed: {
        ...mapState(useUserStore,['userInfo']),
        xmProductCpd(){
            if(this.xmProduct&& this.xmProduct.id){
                return this.xmProduct
            }
            if(this.xmTestCasedb && this.xmTestCasedb.id){
                return {id:this.xmTestCasedb.productId,productName:this.xmTestCasedb.productName}
            }
            if(this.xmMenu && this.xmMenu.id){
                return {id:this.xmMenu.productId,productName:this.xmMenu.productName}
            }
            return {}
        }

    },
    watch:{
        visible(val){
            if(val==true){
                this.initData();
                this.searchTableDatas()
            }
        }
    },
    data() {
        return {
            filters: {
                caseName: '', 
                caseStatus:'',
                testType:'',
            },
             pageInfo:{//分页数据
                total:0,//服务器端收到0时，会自动计算总记录数，如果上传>0的不自动计算。
                pageSize:10,//每页数据
                count:true,//是否需要重新计算总记录数
                pageNum:1,//当前页码、从1开始计算
                orderFields:[],//排序列 如 ['sex','student_id']，必须为数据库字段
                orderDirs:[]//升序 asc,降序desc 如 性别 升序、学生编号降序 ['asc','desc']
            },
             addForm: {
                id:'',caseName:'',caseRemark:'',testStep:'',expectResult:'',menuId:'',menuName:'',ctime:'',ltime:'',luserid:'',lusername:'',cbranchId:'',moduleId:'',moduleName:'',caseStatus:'',cuserid:'',cusername:'',productId:'',verNum:'',casedbId:'',casedbName:''
            },

             editForm: {
                id:'',caseName:'',caseRemark:'',testStep:'',expectResult:'',menuId:'',menuName:'',ctime:'',ltime:'',luserid:'',lusername:'',cbranchId:'',moduleId:'',moduleName:'',caseStatus:'',cuserid:'',cusername:'',productId:'',verNum:'',casedbId:'',casedbName:''
            },
            xmFunc:null,
            apis:{
                add: addXmTestCase,
                list: listXmTestCase,
                del: delXmTestCase,
                batchDel: batchDelXmTestCase,
            }
         }
    },//end data
    methods: { 
         
        preQueryParamCheck(params) {
             
            if(this.xmFunc && this.xmFunc.id){
                params.funcPidPathsLike=this.xmFunc.pidPaths
            }
            if(this.xmTestCasedb && this.xmTestCasedb.id){
                params.casedbId=this.xmTestCasedb.id
            }

            if(this.xmProduct && this.xmProduct.id){
                params.productId=this.xmProduct.id
            }

            if(this.xmMenu && this.xmMenu.menuId){
                params.menuId=this.xmMenu.menuId
            }
            return true;
        },
 
        //显示编辑界面 XmTestCase 测试用例
        showEdit: function ( row,index ) {  
            this.$refs['editDlg'].open({formData:row})
        },
        //显示新增界面 XmTestCase 测试用例
        showAdd: function () { 
            
            this.$refs['addDlg'].open({formData:{}})
        },
         
      editSomeFields(row,fieldName,$event){
        let params={};
        if(this.sels.length>0){
          if(!this.sels.some(k=> k.id==row.id)){
            this.$notify({position:'bottom-left',showClose:true,message:'请编辑选中的行',type:'warning'})
            Object.assign(this.editForm,this.editFormBak)
            return;
          }
            params['ids']=this.sels.map(i=>i.id)
        }else{
            params['ids']=[row].map(i=>i.id)
        }
        if(fieldName=='cuserid'){
            params.cuserid=$event.userid
            params.cusername=$event.username
        }else{
            params[fieldName]=$event
        }

        var func = editSomeFieldsXmTestCase
        func(params).then(res=>{
          let tips = res.tips;
          if(tips.isOk){
            if(this.sels.length>0){
                this.searchTableDatas();
            }
            this.editFormBak=[...this.editForm]
          }else{
            Object.assign(this.editForm,this.editFormBak)
            this.$notify({position:'bottom-left',showClose:true,message:tips.msg,type:tips.isOk?'success':'error'})
          }
        }).catch((e)=>Object.assign(this.editForm,this.editFormBak))
      },
         
        initData: function(){

            if(this.scene){
                this.filters.caseStatus='1'
            }
            this.product=this.xmProduct
        },
        onXmFuncRowClick(row,checked){
            this.xmFunc=checked?row:null 
            this.searchReset()
        },
        onEditFields(params){
            Object.assign(this.editForm,params)
            this.editFormBak={...this.editForm}
        },
         

    },//end methods
    mounted() { 
            this.initData()  
            this.searchTableDatas()
    }
}

</script>

<style scoped />
